const tcb = require("@cloudbase/node-sdk");
const https = require("https");
const app = tcb.init();
const appid = 'wx21a368b891bc616c'
const appSecret = '087bed806ffc4a85d17fe4229ede845e'
const start = async () => {
    var rawData = '';
    return new Promise((resolve, reject) => {
        https.get(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${appSecret}`, (res) => {
            if (res.statusCode == 200) {
                res.on('data', (chunk) => { //接收流数据
                    rawData += chunk;
                });
                res.on('end', () => { //数据接收完毕
                    // 发送从远程服务器获取的响应数据
                    app
                        .uploadFile({
                            cloudPath: "json/token.json",
                            fileContent: Buffer.from(rawData.toString())
                        })
                        .then((res) => {
                            // 返回文件 ID
                            resolve(res.fileID)
                        });
                });
            }


        }).on('error', (e) => {
            resolve(e)
        })
    })



}

module.exports = start
